﻿
using System;
using GNUGo.Net.CLI.CliControls;
using GNUGo.Net.GoTextProtocol;
using GNUGo.Net.Network;
using log4net;
using log4net.Config;

namespace GNUGo.Net.CLI {
    public class Program {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        public static void Main(string[] args) {
            Console.Clear();
            XmlConfigurator.Configure();
            log.Debug("Begin");

            GoOptions options = new GoOptions();
            CommandLineParser parser = new CommandLineParser();
            parser.ParseAndPopulate(args, options);

            // validate command line options
            if ((options.LanAddress != null ? 1 : 0) + (options.LanServer ? 1 : 0) + (options.LocalGtpCommandLine != null ? 1 : 0) > 1) {
                Console.WriteLine(
                    "--lanServer, --lanClient, --local-gtp options cannot be specified at the same time.");
                return;
            }

            CliUserInterface ui = new CliUserInterface();

            // connect to server
            if (options.LanAddress != null) {
                LanClient theLanClient = new LanClient(ui);
                theLanClient.Connect(options.LanAddress, options.LanPort);
            }
            else if (options.LanServer) {
                LanServer theLanServer = new LanServer(ui, options.LanPort);
                theLanServer.BeginGame(options.Size);
            }
            else if (options.LocalGtpCommandLine != null) {
                GtpCommandLineClient gtpCommandLineClient = new GtpCommandLineClient(ui, options.LocalGtpCommandLine);
                gtpCommandLineClient.BeginGame(options.Size);
            }
            else {
                ui.NewGame(options.Size);
            }
            ui.PlayGame();
        }

        private static void PrintHelp() {
            Console.WriteLine("Usage: GNUGo.Net.CLI.exe [options]");
            Console.WriteLine();
            Console.WriteLine(" General Options");
            Console.WriteLine("  --help            This screen");
            Console.WriteLine("  --size=[size]     Specify the size of the board. Can be 9, 13, 19. [default: 9]");
            Console.WriteLine();
            Console.WriteLine(" Network Options");
            Console.WriteLine("  --lan-server      Start a LAN server");
            Console.WriteLine("  --lan-client=[address] Connect to a LAN server");
            Console.WriteLine("  --lan-port=[port] Sets the LAN port [default: 6060]");
            Console.WriteLine();
        }
    }
}
